package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;
    /**
     * 添加分类
     * @param subjectLabelBO
     */
    @Override
    public void add(SubjectLabelBO subjectLabelBO){
        SubjectLabel subjectLabel = SubjectLabelConverter
                .INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabelService.insert(subjectLabel);
    }

    /**
     * 查询分类列表
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelList(SubjectLabelBO subjectLabelBO) {
        List<SubjectLabel> subjectLabels =
                subjectLabelService.queryLabelList(SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO));
        List<SubjectLabelBO> subjectLabelBOS =SubjectLabelConverter.INSTANCE.convertLabelToBO(subjectLabels);
        if(subjectLabelBOS == null || subjectLabelBOS.isEmpty()){
            return Collections.emptyList();
        }
        return subjectLabelBOS;
    }

    @Override
    public void deleteLabel(Long id) {
        subjectLabelService.removeByLabelId(id);
    }

    @Override
    public void update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabelService.updateInfo(subjectLabel);
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果是一级分类那么就查询所有标签

        SubjectMapping subjectMapping = new SubjectMapping() ;
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelIdsByCategory(subjectMapping);
        if(subjectMappingList == null || subjectMappingList.isEmpty()){
            return Collections.emptyList();
        }
        List<Long> labelIds = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labels= subjectLabelService.batchQueryByLabelIds(labelIds);
        log.info("查询分类下的标签: {}", labels);
        List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE.convertLabelToBO(labels);
        log.info("查询分类下的标签: {}", subjectLabelBOList);
        return subjectLabelBOList;
    }

}
